﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;

namespace JZ.Common
{
    public class Logger
    {
        public static void LogException(Exception ex)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Error.log");
            using (TextWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine(string.Format("System crashed at {0}", DateTime.Now.ToLongTimeString()));
                writer.WriteLine(ex.Message);
                writer.WriteLine(ex.StackTrace);
                writer.WriteLine(ex.Source);

                Console.WriteLine(string.Format("System crashed at {0}", DateTime.Now.ToLongTimeString()));
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Source);

                if (ex.InnerException != null)
                {
                    writer.WriteLine("Inner Exception");
                    writer.WriteLine(ex.InnerException.Message);
                    writer.WriteLine(ex.InnerException.StackTrace);
                    writer.WriteLine(ex.InnerException.Source);

                    Console.WriteLine("Inner Exception");
                    Console.WriteLine(ex.InnerException.Message);
                    Console.WriteLine(ex.InnerException.StackTrace);
                    Console.WriteLine(ex.InnerException.Source);
                }
            }
        }

        public static void Log(string logtext)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Error.log");
            using (TextWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine(logtext);
                Console.WriteLine(logtext);
            }
        }

        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="logText"></param>
        public static void WriteErrorLog(string logText)
        {
            ILog log = LogManager.GetLogger("ErrLog");
            if (log.IsErrorEnabled)
            {
                log.Error(logText);
            }
        }

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="logText"></param>
        public static void WriteLog(string logText)
        {
            ILog log = LogManager.GetLogger("NormalLog");
            if (log.IsInfoEnabled)
            {
                log.Info(logText);
            }
        }
    }
}
